﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using car.Models;

namespace car.Controllers
{
    public class VehicleController : Controller
    {
        public IActionResult Index()
        {
            List<Vehicle> list = new List<Vehicle>();
            try
            {
                using (var db=new carContext())
                {
                    list=db.Vehicle.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return View(list);
        }

        public IActionResult Delete(string id)
        {
            try
            {
                using (var db=new carContext())
                {
                    var vehicle = db.Vehicle.Find(id);
                    if(vehicle == null)
                    {
                        ViewData["Message"] = "没有想要删除的项目！";
                        return View("Index");
                    }
                    else
                    {
                        db.Vehicle.Remove(vehicle);
                        db.SaveChanges();
                    }
                }
            }
            catch(Exception ex)
            {
                ViewData["Message"] = "未知错误";
                throw ex;
            }

            return View("Index");
        }
        public IActionResult Create()
        {
            return View();
        }
        public IActionResult Created()
        {
            string id=Request.Form["id"];
            string oh=Request.Form["oh"];
            try
            {
                using (var db = new carContext())
                {
                    var vehicle=db.Vehicle.Find(id);
                    if(vehicle==null)
                    {
                        vehicle=new Vehicle();
                        vehicle.Voh = oh;
                        vehicle.Vid = id;
                        db.Vehicle.Add(vehicle);
                    }
                    else
                    {
                        vehicle.Voh = oh;
                        db.Vehicle.Update(vehicle);
                    }
                    db.SaveChanges(true);
                    ViewBag.flag = true;
                }
            }
            catch (Exception ex)
            {
                ViewBag.flag = false;
                throw ex;
            }
            return View("Create");
        }
        public IActionResult Update(string id)
        {
            ViewData.Clear();
            ViewData["Vid"] = id;
            return View();
        }
        public IActionResult Updated(string id)
        {
            try
            {
                using(var db = new carContext())
                {
                    var vehicle = db.Vehicle.Find(id);
                    if(vehicle==null)
                    {
                        ViewData["Flag"] = "未找到";
                        return View("Index");
                    }
                    else
                    {
                        vehicle.Voh = Request.Form["oh"];
                        db.Vehicle.Update(vehicle);
                        db.SaveChanges();
                        ViewData["Flag"] = "succeed";
                    }
                }
            }
            catch(Exception ex)
            {
                ViewData["Flag"] = "false";
                throw ex;

            }
            return View("Update");
        }
        [HttpPost]
        public IActionResult SearchID(string name)
        {
            var subscribelist = new carContext();
            var filelist = subscribelist.Vehicle.ToList();
            if (!string.IsNullOrEmpty(name))
                filelist = filelist.Where(p => p.Vid.Contains(name)).ToList();
            return View("Index", filelist);
        }
    }
}
